package cn.tedu.ivos.dict.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.dict.pojo.dto.DictSaveParam;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dict.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v1/dict")
public class DictController {
    @Autowired
    private DictService dictService;
    @GetMapping("/select")
    public JsonResult selectDict(DictQuery dictQuery){
        log.debug("开始处理查询字典请求:"+dictQuery);
        List<DictVO> dictVOList =dictService.selectDict(dictQuery);
        return JsonResult.ok(dictVOList);
    }
    @PostMapping("/save")
    public JsonResult saveDict(@RequestBody DictSaveParam dictSaveParam){
        log.debug("开始处理保存字典请求:"+ dictSaveParam);
        int row = dictService.saveDict(dictSaveParam);
        if(row==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"保存失败");
        }
        return JsonResult.ok();
    }
    @PostMapping("/delete/{id}")
    public JsonResult deleteDict(@PathVariable Long id){
        log.debug("开始处理删除字典请求:"+id);
        int row = dictService.deleteDict(id);
        if(row==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"删除失败");
        }
        return JsonResult.ok();
    }
}
